package aries.utils;

/******************************************************************************
 * You may modify, publish, and use Aries for your own purpose. BUT
 * DON'T REMOVE THIS TEXT.
 * Visit my facebook: http://www.facebook.com/freeskies or
 * Follow my twitter: @freeskys
 *
 *
 * Author: Harditya Rahmat Ramadhan
 * Licence: GPLv2
 *****************************************************************************/

import aries.Setting;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil extends Util {
    
    /**
     * Increment weeks parameter.
     */
    public static final byte WEEKS = 0;
    
    /**
     * Increment hours parameter.
     */
    public static final byte HOURS = 1;
    
    /**
     * Increment minutes parameter.
     */
    public static final byte MINUTES = 2;
    
    /**
     * Increment seconds parameter.
     */
    public static final byte SECONDS = 3;
    
    /**
     * Get year for used in combobox.
     * 
     * @param limit
     * @param start
     * @return String[]
     */
    public static String[] getYear(int limit, String start) {
        String[] date = new String[limit];
        date[0] = start;
        int theStart = Integer.parseInt(start)-1;
        for (byte i=1; i<limit; i++) {
            date[i] = String.valueOf(theStart);
            theStart--;
        }
        return date;
    }
    
    /**
     * Get month to be used in combobox.
     * Don't forget to set the Aries Language via Setting class.
     */
    public static String[] getMonth() {
        return new String[] {Setting.getCurrentLanguage().getText("month.january"), 
        Setting.getCurrentLanguage().getText("month.february"),
        Setting.getCurrentLanguage().getText("month.march"),
        Setting.getCurrentLanguage().getText("month.april"),
        Setting.getCurrentLanguage().getText("month.may"),
        Setting.getCurrentLanguage().getText("month.june"),
        Setting.getCurrentLanguage().getText("month.july"),
        Setting.getCurrentLanguage().getText("month.august"),
        Setting.getCurrentLanguage().getText("month.september"),
        Setting.getCurrentLanguage().getText("month.october"),
        Setting.getCurrentLanguage().getText("month.november"),
        Setting.getCurrentLanguage().getText("month.december")};
    }
    
    /**
     * Convert from string to date.
     * 
     * @param date
     * @return Date
     * @throws ParseException 
     */
    public static Date stringToDate(String date) throws ParseException {
        return DateFormat.getDateInstance().parse(date);
    }
    
    //@TODO: add current date method
    
    /**
     * Add the calendar with hours.
     * 
     * @param cal
     * @param hours
     * @return 
     */
    public static Date increment(byte type, Calendar cal, int increment) {
        Date result = null;
        if (type == WEEKS) {
            cal.add(Calendar.WEEK_OF_YEAR, increment);
            return cal.getTime();
        } else if (type == HOURS) {
            cal.add(Calendar.HOUR, increment);
            return cal.getTime();
        } else if (type == MINUTES) {
            cal.add(Calendar.MINUTE, increment);
            return cal.getTime();
        } else if (type == SECONDS) {
            cal.add(Calendar.SECOND, increment);
            return cal.getTime();
        }
        return result;
    }
    
    /**
     * Subtract the calendar with hours.
     * 
     * @param cal
     * @param hours
     * @return Date
     */
    public static Date decrement(byte type, Calendar cal, int decrement) {
        Date result = null;
        if (type == WEEKS) {
            cal.add(Calendar.WEEK_OF_YEAR, -decrement);
            return cal.getTime();
        } else if (type == HOURS) {
            cal.add(Calendar.HOUR, -decrement);
            return cal.getTime();
        } else if (type == MINUTES) {
            cal.add(Calendar.MINUTE, -decrement);
            return cal.getTime();
        } else if (type == SECONDS) {
            cal.add(Calendar.SECOND, -decrement);
            return cal.getTime();
        }
        return result;
    }
    
    /**
     * Check if first is after second.
     * 
     * @param first
     * @param second
     * @return Boolean
     */
    public static Boolean isAfter(Date first, Date second) {
        return first.after(second);
    }
    
    /**
     * Check if first is before second.
     * 
     * @param first
     * @param second
     * @return Boolean
     */
    public static Boolean isBefore(Date first, Date second) {
        return first.before(second);
    }
    
    /**
     * Get current date.
     * You can get Date(dd/MM/yyyy) or Time(HH:mm:ss).
     * The type is Calendar.DATE or Calendar.TIME
     * 
     * @param type
     * @param format
     * @return String
     */
    public static String getCurrentDate(int type, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date = new Date();
        return simpleDateFormat.format(date);
    }
    
    /**
     * Get current weeks in month or in year.
     * 
     * @param type
     * @return int
     */
    public static int getCurrentWeeks(int type) {
        Calendar cal = Calendar.getInstance();
        return cal.get(type);
    }
        
}
